ad space

How to transfer data from NodeMCU via WiFi?

nodemcu interfacing with DHT11

Here it is shown how to read sensor data and transfer it from NodeMCU ESP8266MOD board to PC or mobile or other device browser using WiFi connection. This is required if you want acquire sensor data or other kinds of data and send it through wifi to local server or post data on the internet. This allows one to create Internet of Things(IoT) devices and IoT projects. In this demonstration we will acquire humidity and temperature data from DHT11 sensor using NodeMCU board and then send that sensor data to the PC wirelessly via WiFi network. 

Hardware and Interfacing

Here NodeMCU ESP8266 board is used which is microcontroller board with wifi capability. The following shows the NodeMPCU ESP8266MOD board.

NodeMCU ESP8266 board pinout
The humidity and temperature sensor used here is the DHT11 which has four pins but the DHT sensor breakout board has three pins. Here we will use the DHT sensor breadboard. This DHT sensor board has Vcc pin, digital output pin and the ground pin. Here we will connect the DHT11 sensor digital pin to GPIO05(digital pin D1) of the nodemcu board. 
 
The following shows DHT11 sensor module.
 The following circuit diagram shows how to connect the digital output pin of DHT11 sensor to the GPIO05(digital pin D1) of NodeMCU board.
NodeMCU interfacing with DHT11 sensor

The DHT11 sensor Vcc pin is connected to the 3.3V pin of the nodemcu board and the ground pin is connected to the ground pin of the nodemcu board. Thus the DHT11 sensor module is powered using the NodeMCU board 3.3V.

The following shows the Nodemcu board attached to a breadboard and connect to the DHT11.

nodemcu interfacing with DHT11

To power the NodeMCU, 5V regulated power supply was used which is connected to the Vin pin of the NodeMCU board. This is shown below.

nodemcu with power supply

Software & Programming

The NodeMCU is programmed using Arduino IDE(which is open-source and free to download). Aside from the Arduino IDE we need to install two libraries(wifi & DHT11) and one header file(aRest). 

1. DHT11 sensor library

This library makes it easy for us for writing code for control and data acquisition from the sensor. It can be installed via the library manager in Arduino IDE. The tutorial DHT11 Humidity Temperature Sensor with Arduino explains how to install this library.

2. WiFi library

 With this library we can create wifi connection and makes wifi programming easier. This library can also be installed via the library manager in Arduino IDE. How to install this wifi library is explained in the tutorial LED Blink using ESP8266 & Arduino IDE with Video and Pictures.

3. aRest header file

This header file is required to send the data from the NodeMCU to the PC, mobile handset or any other device browser. That is REST framework is used to send data via the WiFi connection. This header file can be downloaded from the following link.

https://github.com/marcoschwartz/aREST/blob/master/aREST.h

You are read more about the aREST framework at the following link:

https://www.arduino.cc/reference/en/libraries/arest

This header file should be located in the same folder as the Arduino sketch file which has .ino file extension.

Source Code

The following is the complete code to read and transfer sensor data from NodeMCU to PC or any device browser.


// Import required libraries
#include "ESP8266WiFi.h"
#include "aREST.h"
#include "DHT.h"

// WiFi parameters
const char* ssid = "yourWiFiSSID";
const char* password = "yourWiFipassword";

// declare port number for listening incoming TCP connections 
#define LISTEN_PORT 80

// Create an instance of the server
WiFiServer server(LISTEN_PORT);

// Create aREST instance
aREST rest = aREST();

// DHT11 sensor pins
#define DHTPIN 4
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// Variables to hold data from DHT11 sensor
float humidity;
float temperature;


void setup(void)
{  
  // Start Serial
  Serial.begin(9600);
  
  // Init DHT 
  dht.begin();
  
  // Initialize variables for REST API
  rest.variable("humidity",&humidity);
  rest.variable("temperature",&temperature);
    
  // Give name and ID to device
  rest.set_id("1");
  rest.set_name("esp8266");
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
  
  // Show IP address
  Serial.println(WiFi.localIP());
  
}

void loop() {
    
  // Reading temperature and humidity
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  
  // Handle REST calls
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while(!client.available()){
    delay(1);
  }
  rest.handle(client);
 
}

The source code is now explained.

1. First we import the required libraries

#include "ESP8266WiFi.h"
#include "aREST.h"
#include "DHT.h"

 2. The following lines of code are required to setup the WiFi connection. First we save the wifi credentials in variables ssid and password. Here you must replace the yourWiFiSSID and yourWiFipassword with your actual WiFi SSID and password. The alias MYPORT is used to hold the port number 80 which is http port where the sensor data is visible along with the URL. Then we create an instant of WiFi server wherein we pass the port number.

// WiFi parameters
const char* ssid = "yourWiFiSSID";
const char* password = "yourWiFipassword";

// declare port number for listening incoming TCP connections 
#define MYPORT 80

// Create an instance of the server
WiFiServer server(MYPORT);

3. The following line of code is used for DHT11 sensor. First we create alias the DHT11 sensor pin and the type of DHT sensor which can be either DHT11 or DHT22. Here we are using DHT11 sensor. Then we have initialized the DHT sensor. Also two variables of float type called humidity and temperature are created which will hold the humidity and temperature data from the sensor.

// DHT11 sensor pins
#define DHTPIN 4
#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// Variables to hold data from DHT11 sensor
float humidity;
float temperature;

4. Then we create aREST instance called rest using the aREST() function using the following line. Later on we can use various methods using this rest instance.

// Create aREST instance
aREST rest = aREST();

 5. In the setup() loop we have first setup the serial connection with the following line of code. This is used to read the WiFi connection status. We can use Arduino serial monitor to check the WiFi connection or we can use any other serial terminal such as Tera Term.

// Start Serial
  Serial.begin(9600);

6. The following code is for initialization of the DHT sensor.

// Init DHT 
  dht.begin();

7. The following code is for REST API which is used for data transfer over WiFi(TCP). The rest is the aREST instance and variable(), set_id() and set_name() are its methods. The methods are accessed by the instance using the dot notation.

// Initialize variables for REST API
  rest.variable("humidity",&humidity);
  rest.variable("temperature",&temperature);
    
  // Give name and ID to device
  rest.set_id("1");
  rest.set_name("esp8266");

8. The next lines of codes are for making and checking WiFi connection between the NodeMCU and the WiFi access point.

 // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
  
  // Show IP address
  Serial.println(WiFi.localIP());

9. In the loop() we read the sensor humidity and temperature data and store them in the variables humidity and temperature created earlier.

// Reading temperature and humidity
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();

10. In the final step we create a WiFi client called client(the NodeMCU) and check whether it is connected to the server(the WiFi access point router). If there is no wifi connection or there is any client creation error we abort. If there is connection and client is available we create a 1 ms delay. The rest.handle(client) is used to transfer data from NodeMCU to the WiFi server. Between the data transfer there is 1ms delay if there is client available.

// Handle REST calls
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while(!client.available()){
    delay(1);
  }
  rest.handle(client);

Results

After you have uploaded the code to the NodeMCU and when you open the Arduino serial monitor you should see that WiFi is connected, server has started and the IP address of the client provided by the DHCP server.

Then if you open browser like Firefox and type in the http://192.168.1.8/humidity you should see the humidity data from the sensor, the given id and name of the device and the connection status. This is as shown below.

Similarly the temperature sensor data can be viewed on the browser on the url http://192.168.1.8/temperature.

JSON Restful Humidity Data on browser

The raw JSON format data is displayed in google chrome browser(or Firefox if you choose to see raw data) like the following.

humidity data on google chrome

 You can use any device such as mobile phone and type in the url to see the humidity and temperature data.

Further recommendation and tutorials

Also you can setup standalone home server to create website and display the sensor data there. See how to host website from home if you are interested in this. Or you can make the sensor data publicly available on the internet, an example of which is shown in the tutorial how to display Sensor data in real time on Web. Similarly one can control devices over the internet(which is called Internet of Things) such as DC motors and other devices. An example of this is shown in the tutorial WiFi controlled DC motor with ESP12E.

Post a Comment

Previous Post Next Post